﻿//-----------------------------------------------------------------------
// <copyright file="ActivityFeed.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation 2011.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Yogesh M Joshi</author>
// <date>02-10-2011</date>
// <summary></summary>
//-----------------------------------------------------------------------

namespace SPActivityFeed.WebParts
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using SPActivityFeed.DataSources;
    using SPActivityFeed.Utility;

    /// <summary>
    /// Displays the activity feed for current user
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class ActivityFeed : WebPart, ICallbackEventHandler
    {

        /// <summary>
        /// Acitivty Feed Data Source
        /// </summary>
        private ActivityFeedDataSource activityFeedDataSource;

        /// <summary>
        /// Current webpart div id
        /// </summary>
        private string activityFeedDivId = string.Empty;

        /// <summary>
        /// Gets or sets the XSL Transformation.
        /// </summary>
        /// <value>The XSL Transformation.</value>
        [WebBrowsable]
        [Personalizable]
        [WebDisplayName("XSL Transformation")]
        [WebDescription("The XSL Transformation to render webpart contents.")]
        [Category("Activity Feed")]
        public string XSLTransformation
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the XSLT location.
        /// </summary>
        /// <value>The XSLT location.</value>
        [WebBrowsable]
        [Personalizable]
        [WebDisplayName("Maximum Events To Display")]
        [WebDescription("The number of events to display.")]
        [Category("Activity Feed")]
        public int MaxEvents
        {
            get;
            set;
        }

        /// <summary>
        /// Return the callback result
        /// </summary>
        /// <returns>Activity feed HTML</returns>
        public string GetCallbackResult()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            Common.RenderHtml(writer, this.activityFeedDataSource.GetDataXml(), this.XSLTransformation, Constants.SampleActivityFeedXslt);
            return sb.ToString();
        }

        /// <summary>
        /// Raise the callback event
        /// </summary>
        /// <param name="eventArgument">Event argument</param>
        public void RaiseCallbackEvent(string eventArgument)
        {
            // throw new NotImplementedException();
        }

        /// <summary>
        /// Load the web part and initialize the data source
        /// </summary>
        /// <param name="e">Event argument</param>
        protected override void OnLoad(EventArgs e)
        {
            Common.RegisterWebResource(this.Page, this.GetType(), Constants.ActivityFeedCss);
            Common.RegisterScriptResource(this.Page, this.GetType(), Constants.ActivityFeedJs);

            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            this.activityFeedDataSource = new ActivityFeedDataSource(currentUser);
            this.activityFeedDataSource.LoadActivitiesForMe(this.MaxEvents);

            ClientScriptManager csm = Page.ClientScript;
            string callbackEventReference = csm.GetCallbackEventReference(this, "args", "LoadActivityFeedCallback", "context", true);
            this.Attributes.Add("callbackEventReference", callbackEventReference);

            string clientScript = "_spBodyOnLoadFunctionNames.push(\"LoadActivityFeed('" + this.activityFeedDivId + "')\");";
            csm.RegisterClientScriptBlock(this.GetType(), "LoadActivityFeed_" + this.activityFeedDivId, clientScript, true);

            base.OnLoad(e);
        }

        /// <summary>
        /// Initialize the web part
        /// </summary>
        /// <param name="e">Event arguments</param>
        protected override void OnInit(EventArgs e)
        {
            this.activityFeedDivId = this.ClientID;
            base.OnInit(e);
        }

        /// <summary>
        /// Add the waiting animation
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Control img = Common.GetWaitingImage();
            this.Controls.Add(img);
        }
    }
}
